package com.manor.paradise.dialog

import android.content.Context
import android.text.*
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.bean.GiftsBean
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_telephone.view.*
import java.math.BigDecimal


/**
 * 兑换手机话费
 * @property data RecordsBean
 * @constructor
 */
class TelephoneChargesPopup(context: Context, var data: GiftsBean.RecordsBean) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_telephone
    }

    override fun onCreate() {
        super.onCreate()
        tv_name.setText(data.name.toString())
        im_dismiss.setOnClickListener(this)
        ed_tel.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(11))) //最大输入长度
        ed_password.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_password.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        tv_mondy.setText("1")
        ed_sdj.setText(data.sdCoupons.toString())
        tv_mondy.addTextChangedListener( object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                    if(!TextUtils.isEmpty(tv_mondy.text.toString())){
                        ed_sdj.setText(BigDecimal(tv_mondy.text.toString()).multiply(data.sdCoupons).toString())
                    }else{
                        ed_sdj.setText("")
                    }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })

        tv_confirm.setOnClickListener({
            if (!TextUtils.isEmpty(ed_tel.text.toString()) && !TextUtils.isEmpty(ed_password.text.toString())&&ed_password.text.toString().length==6 ) {
                ElvesHttpUtil.getGiftBuy(
                        data.id,
                        ed_tel.text.toString(),
                        BigDecimal("1"),
                        BigDecimal(data.sdCoupons.toString()),
                        ed_password.text.toString(),
                        data.isBlogistics,
                        "",
                        "",
                        object : JHttpCallback() {
                            override fun onSuccess(code: Int, msg: String, info: String) {
                                ToastUtil.show(msg)
                                if (code != 200) {

                                    return
                                }

                                dismiss()

                            }

                            override fun onError() {
                                super.onError()

                            }
                        })
            }else{
                if(ed_tel.text.toString().length<6){
                    ToastUtil.show("请输入手机号")
                }
                if(ed_password.text.toString().length<6){
                    ToastUtil.show("请输入6位交易密码")
                }
                if(TextUtils.isEmpty(ed_password.text.toString())){
                    ToastUtil.show("请输入交易密码")
                }
            }
        })

        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有淘米劵：" + paradiseInfoBean.sdCoupons.toString())

            }

            override fun onError() {
                super.onError()

            }
        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_GIFT_BUY)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }
}